/*
* @Author: WeiLin
* @Date:   2019-04-02 13:14:48
 * @Last Modified by: LiaoWeiLin
 * @Last Modified time: 2019-06-03 13:58:15
*/

var gulp = require('gulp');
var rev = require('gulp-rev'); //hash
var revReplace = require('gulp-rev-replace');
var useref = require('gulp-useref');
var filter = require('gulp-filter');
var uglify = require('gulp-uglify'); // 压缩js
var csso = require('gulp-csso');  // 压缩css

var htmlmin = require('gulp-htmlmin');

gulp.task('default', function() {

	var jsFilter = filter('**/*.js', {restore: true});
	var cssFilter= filter('**/*.css', {restore: true});

	var htmlFilter = filter('**/*.html', {restore: true});

	var options = {
		removeComments: true,//清除HTML注释
		collapseWhitespace: true,//压缩HTML
		removeEmptyAttributes: true,//删除所有空格作属性值
		removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
		minifyJS: true, // 压缩js
        minifyCSS: true //压缩页面CSS
	}

	return gulp.src('src/*.html')
		.pipe(useref())
		.pipe(jsFilter)
		.pipe(uglify())
		.pipe(jsFilter.restore)
		.pipe(cssFilter)
		.pipe(csso())
		.pipe(cssFilter.restore)
		.pipe(htmlFilter)
		.pipe(htmlmin(options))
		.pipe(htmlFilter.restore)
		.pipe(revReplace())
		.pipe(gulp.dest('dist'));
})